<?php

class Homepage_AdminController extends Ac_AdminController
{

    public function dashboardAction()
    {
        $this->view->num_active_user = Model_Category::all(array('sa' => array('$exists' => 0)))->count();
        $this->view->num_banned_user = Model_Category::all(array('sa' => Model_Listing::STATUS_BANNED))->count();
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {
        $_GET['sort'] = $sort = !empty($_GET['sort']) ? $_GET['sort'] : 'recent';
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $per_page  = 50;
        $query = '';
        $fields = array();
        $additionalParameters = array();

        if (!empty($_GET['id'])) {
            $cat = Model_Category::find($_GET['id']);
            $list = $cat->getChildren();
            $this->view->category = $cat;
        } else {
            $list = Model_Category::getLevel1();
        }
        //$this->view->list    = Model_Listing::all()->limit(50);
        if ($page>1) {
            $list->skip(($_GET['page']-1) * $per_page);
        }
        $this->view->list           = $list->limit($per_page)->sort(array('nav_pos'=>1));
        $this->view->listing_number = $list->count();
        $this->view->page           = $page;
        $this->view->per_page       = $per_page;
        $this->render('list');
    }

    public function editAction()
    {
        
        $id = $_POST['edit_id'];
        $classname = 'Model_Category';
        /*
        if (!empty($_POST['customization'])) {
            $classname = 'Model_Category_'.$_POST['customization'];
        } else {
            $classname = 'Model_Category';
        }*/
        $class = new $classname;
        $cat = $class::getMongoCollection(false)->findOne(array('_id' => new MongoId($id)));
        //$cat   = $class::find($id);
        //$cat->_type = array('Model_Category');
        $cat['nav_name']             = $_POST['name'];
        $cat['id']                   = $_POST['handle'];
        $cat['homepage']             = (int) $_POST['homepage'];

        $class::getMongoCollection(false)->save($cat);
        echo 'ok';exit;
    }

    public function homepageAction()
    {
        $listing = Model_Category::find($_GET['id']);
        if (!empty($_GET['act'])) {
            $listing->homepage = 1;
        } else {
            unset($listing->homepage);
        }
        $listing->save();
        if ($this->_request->isXmlHttpRequest()) {
            echo (int)!empty($_GET['act']) ;exit;
        } else {
            $this->_redirect($this->view->geturl('list','category', null, 'admin1', array('id'=> $_GET['back'])));
        }
    }


    public function positionAction()
    {
        foreach($_POST['data'] as $it) {
            $cat = Model_Category::find($it['id']);
            $cat->nav_pos = (int)$it['pos'];
            $cat->save();
        }
        echo 'ok';exit;
    }
}